define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            $(".btn-add").data('area',["100%",'100%']);
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'sales/checkout/index' + location.search,
                    // add_url: 'sales/checkout/add',
                    // edit_url: 'sales/checkout/edit',
                    // del_url: 'sales/checkout/del',
                    // multi_url: 'sales/checkout/multi',
                    // table: 'sales_checkout',
                }
            });
            $('.btn-export').click(function () {
                var url = Config.this_url+'/sales/checkout/export'
                layer.open({
                    type: 2, // 1-在content中写html代码， 2-在content中写src路径
                    area: ['100%', '100%'],
                    // area: ['20%', '60%'],
                    title: "导出选择",
                    content: url,
                    // content: Config.this_url+'/purchase/requisition',
                })
            })
            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                rowStyle:function(row, index){
                    if (row.ystatus == 6) {
                        return{
                            css:{
                                color:'red'
                            }
                        }
                    }else{
                        return{}
                    }
                },
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),class:'cid'},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'number', title: __('Number')},
                        {field: 'slip.number', title: __('slip.Number')},

                        // {field: 'collectiontime', title: __('Collectiontime'),operate:false,  addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'address', title: __('Address'),class:'address',operate:false,cellStyle:function (value, row, index) {
                                return {
                                    css: {
                                        "min-width": "100px",
                                        "white-space": "nowrap",
                                        "text-overflow": "ellipsis",
                                        "overflow": "hidden",
                                        "max-width": "120px"
                                    }
                                }
                            }},

                        // {field: 'abstract', title: __('Abstract')},
                        {field: 'operator', title: __('Operator')},
                        {field: 'company', title: __('Company'),operate:false},
                        {field: 'actual_cost', title: __('actual_cost'),operate:false},
                        {field: 'tax', title: __('Tax'),operate:false},
                        {field: 'customer.company', title: __('Customer.company'),operate:'LIKE', formatter:Table.api.formatter.search},
                        {field: 'storehouse.name', title: __('Storehouse.name'),searchList: $.getJSON(Config.this_url+"/config/zaxconfig?states=1"),defaultValue:'1'},
                        {field: 'status', title: __('Status'),searchList: $.getJSON(Config.this_url+"/config/zaxconfig?states=9")},
                        {field: 'explain', title: __('Explain'),operate:false,class: 'explain',cellStyle:function (value, row, index) {
                                return {
                                    css: {
                                        "min-width": "100px",
                                        "white-space": "nowrap",
                                        "text-overflow": "ellipsis",
                                        "overflow": "hidden",
                                        "max-width": "120px"
                                    }
                                }
                            }},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            buttons: [
                                {
                                    name: 'examine2',
                                    title: __('发货'),
                                    classname: 'btn btn-xs btn-error btn-click editstatus',
                                    icon: 'fa fa-sign-out',
                                    hidden:function(row){
                                        if(row.ystatus != 2){
                                            return true;
                                        }
                                    },
                                    click: function(data){
                                        var cid =$(this).parents('tr').find('.cid').html()
                                        var url = Config.this_url+'/sales/checkout/examine2?ids='+cid;
                                        layer.open({
                                            type: 2, // 1-在content中写html代码， 2-在content中写src路径
                                            area: ['80%', '90%'],
                                            title: "出库单",
                                            content: url,
                                            // content: Config.this_url+'/purchase/requisition',
                                        })
                                    }},
                                {
                                    name: 'examine3',
                                    title: __('到货'),
                                    classname: 'btn btn-xs btn-error btn-click editstatus',
                                    icon: 'fa fa-truck',
                                    hidden:function(row){
                                        if(row.ystatus != 3){
                                            return true;
                                        }
                                    },
                                    click: function(data){
                                        var cid =$(this).parents('tr').find('.cid').html()
                                        var url = Config.this_url+'/sales/checkout/examine3?ids='+cid;
                                        layer.open({
                                            type: 2, // 1-在content中写html代码， 2-在content中写src路径
                                            area: ['80%', '90%'],
                                            title: "到货凭证",
                                            content: url,
                                            // content: Config.this_url+'/purchase/requisition',
                                        })
                                    }},
                                {
                                    name: 'export',
                                    title: __('导出'),
                                    classname: 'btn btn-xs btn-warning btn-click editstatus',
                                    icon: 'fa fa-upload',
                                    hidden:function(row){
                                        // if(row.ystatus != 3){
                                        //     return true;
                                        // }
                                    },
                                    click: function(data){
                                        var cid =$(this).parents('tr').find('.cid').html()
                                        var url = Config.this_url+'/sales/checkout/exportdetail?ids='+cid
                                        window.open(url)
                                    }},
                                {
                                    name: 'printing',
                                    title: __('打印'),
                                    classname: 'btn btn-xs btn-warning btn-click editstatus',
                                    icon: 'fa fa-print',
                                    hidden:function(row){

                                    },
                                    click: function(data){
                                        var cid =$(this).parents('tr').find('.cid').html()
                                        var url = Config.this_url+'/sales/checkout/printing?ids='+cid
                                        window.open(url)
                                    }},
                                {
                                    name: 'examine',
                                    title: __('出库单审核'),
                                    classname: 'btn btn-xs btn-success btn-click editstatus',
                                    icon: 'fa fa-random',
                                    hidden:function(row){
                                        if(row.ystatus != 1){
                                            return true;
                                        }
                                    },
                                    click: function(data){
                                        var cid =$(this).parents('tr').find('.cid').html()
                                        var url = Config.this_url+'/sales/checkout/detail?ids='+cid
                                        layer.open({
                                            type: 2, // 1-在content中写html代码， 2-在content中写src路径
                                            area: ['80%', '90%'],
                                            title: "出库详情",
                                            content: url,
                                            // content: Config.this_url+'/purchase/requisition',
                                        })
                                    }},
                                {
                                    name: 'retreat',
                                    title: __('销售退货'),
                                    classname: 'btn btn-xs btn-success btn-click editstatus',
                                    icon: 'fa fa-level-up',
                                    hidden:function(row){
                                        if(row.ystatus != 4){
                                            return true;
                                        }
                                    },
                                    click: function(data){
                                        var cid =$(this).parents('tr').find('.cid').html()
                                        var url = Config.this_url+'/sales/checkout/retreat?ids='+cid
                                        layer.open({
                                            type: 2, // 1-在content中写html代码， 2-在content中写src路径
                                            area: ['100%', '100%'],
                                            title: "退货",
                                            content: url,
                                            // content: Config.this_url+'/purchase/requisition',
                                        })
                                    }},
                                {
                                    name: 'change',
                                    title: __('销售换货'),
                                    classname: 'btn btn-xs btn-success btn-click editstatus',
                                    icon: 'fa fa-exchange',
                                    hidden:function(row){
                                        if(row.ystatus != 4){
                                            return true;
                                        }
                                    },
                                    click: function(data){
                                        var cid =$(this).parents('tr').find('.cid').html()
                                        var url = Config.this_url+'/sales/checkout/change?ids='+cid
                                        layer.open({
                                            type: 2, // 1-在content中写html代码， 2-在content中写src路径
                                            area: ['80%', '90%'],
                                            title: "换货",
                                            content: url,
                                            // content: Config.this_url+'/purchase/requisition',
                                        })
                                    }},
                                // {
                                //     name: 'hongchong',
                                //     title: __('红冲'),
                                //     classname: 'btn btn-xs btn-danger btn-click editstatus',
                                //     icon: 'fa fa-xing',
                                //     hidden:function(row){
                                //         if(row.ystatus <2 || row.ystatus >4){
                                //             return true;
                                //         }
                                //     },
                                //     click: function(data){
                                //         var cid =$(this).parents('tr').find('.cid').html()
                                //         layer.confirm('是否红冲该单据？', {
                                //             btn: ['确定','取消'] //按钮
                                //         }, function(){
                                //             var url = Config.this_url+'/sales/checkout/hongchong?ids='+cid
                                //             $.post(url,{},function (res) {
                                //                 layer.msg(res.msg)
                                //                 if(res.code == 1){
                                //                     setInterval(function () {
                                //                         window.location.reload()
                                //                     },1000)
                                //                 }
                                //             })
                                //         })
                                //     }},
                                // {
                                //     name: 'endcheckout',
                                //     title: __('强制终止'),
                                //     classname: 'btn btn-xs btn-danger btn-click editstatus',
                                //     icon: 'fa fa-close',
                                //     hidden:function(row){
                                //         if(row.ystatus != 2){
                                //             return true;
                                //         }
                                //     },
                                //     click: function(data){
                                //         var cid =$(this).parents('tr').find('.cid').html()
                                //         layer.confirm('是否强制终止该单据？', {
                                //             btn: ['确定','取消'] //按钮
                                //         }, function(){
                                //             var url = Config.this_url+'/sales/checkout/endcheckout?ids='+cid
                                //             $.post(url,{},function (res) {
                                //                 layer.msg(res.msg)
                                //                 if(res.code == 1){
                                //                     setTimeout(function () {
                                //                         $('.fa-refresh').click()
                                //                     },1000)
                                //                 }
                                //             })
                                //         })
                                //     }}
                            ],
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });
            $('body').on('mouseenter','.address',function () {
                if (this.offsetWidth < this.scrollWidth) {
                    var that = this;
                    var text = $(this).text();
                    layer.tips(text, that,{
                        tips: 1,
                        time: 2000         //设置显示时间
                    });
                }
            })
            $('table').on('dblclick','td',function(){
                var cid = $(this).parents('tr').find('.cid').html()
                var url = Config.this_url+'/sales/checkout/detail?ids='+cid
                layer.open({
                    type: 2, // 1-在content中写html代码， 2-在content中写src路径
                    area: ['80%', '90%'],
                    title: "出库详情",
                    content: url,
                    // content: Config.this_url+'/purchase/requisition',
                })
            });
            $('body').on('mouseenter','.explain',function () {
                if (this.offsetWidth < this.scrollWidth) {
                    var that = this;
                    var text = $(this).text();
                    layer.tips(text, that,{
                        tips: 1,
                        time: 2000         //设置显示时间
                    });
                }
            })
            $('#myTabContent').on('click','.btn-gather',function(){
                var url = Config.this_url+'/sales/checkout/gather'
                layer.open({
                    type: 2, // 1-在content中写html代码， 2-在content中写src路径
                    area: ['30%', '50%'],
                    title: "查询",
                    content: url,
                    // content: Config.this_url+'/purchase/requisition',
                })
            })
            $('#getcount').click(function () {
                var data = $('form').serialize();
                that = $(this)
                var url = Config.this_url+'/sales/checkout/getcount';
                $.post(url,data,function (res) {
                    that.find('#num').html(res.num)
                    that.find('#price').html(res.price)
                })
                setTimeout(function(){
                    layer.closeAll('loading');
                }, 2000);
            })
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        detail: function () {
            $('.cancelbill').click(function () {
                var url =  Config.this_url+'/sales/checkout/cancelbill'+location.search
                layer.prompt({title: '输入红冲原因', formType: 2}, function(pass, index){
                    if(pass == ''){
                        layer.msg('不能为空');
                        return false;
                    }
                    $.post(url,{reason:pass},function (res) {
                        layer.msg(res.msg)
                        if(res.code == 1){
                            layer.close(index);
                            setTimeout(function () {
                                location.reload();
                            }, 800);
                            window.parent.$(".toolbar .btn-refresh").trigger('click');
                        }
                    })
                });
            })
            Controller.api.bindevent();
        },
        retreat: function () {
            Controller.api.bindevent();
        },
        examine2: function () {
            Controller.api.bindevent();
        },
        examine3: function () {
            Controller.api.bindevent();
        },
        export: function () {
            $('.btn-embossed').click(function () {
                // console.log($(".checkstatus").val())
                // console.log($("input[ name='start_time']").val())
                // console.log($("input[ name='end_time']").val())
                window.location.href = Config.this_url+'/sales/checkout/exportExcel?status='+$(".checkstatus").val()+'&start_time='+$("input[ name='start_time']").val()+'&end_time='+$("input[ name='end_time']").val()+'&customer_id='+$("#c-customer_id").val()+'&storehouse_id='+$("#c-storehouse_id").val()
                return false;
            })
            Controller.api.bindevent();
        },
        change: function () {
            $(".btn-add").click(function () {
                var house_id
                layer.open({
                    type: 2, // 1-在content中写html代码， 2-在content中写src路径
                    area: ['70%', '90%'],
                    title: "选择商品",
                    content: Config.this_url+'/sales/slip/seach?house_id='+$('#c-storehouse_id').val(),
                })
            })
            Controller.api.bindevent();
        },
        gather: function () {
            $('.btn-embossed').click(function () {
                var turl = Config.this_url+'/sales/checkout/gatherres?number='+$('#c-goods_id').val()+'&storehouse_id='+$('#c-storehouse_id').val()+'&customer_id='+$('.customer_id').val()+'&starttime='+$('.start_time').val()+'&endtime='+$('.end_time').val()+'&type='+$("input[ name='type']").filter(':checked').val()
                // console.log(turl)
                Backend.api.addtabs(turl, '汇总查询', 'fa fa-circle-o fa-fw');
            })
            Controller.api.bindevent();
        },
        gatherres:function(){
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'sales/checkout/gatherres' + location.search,
                    table: 'sales_slip',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                pageSize:15,
                searchFormVisible: true,
                searchFormTemplate: 'customformtpl',
                // commonSearch: false,
                visible: false,
                showToggle: false,
                showColumns: false,
                search:false,
                // showExport: false,
                columns: [
                    [
                        {checkbox: true,operate:false},
                        {field: 'id', title: 'ID', operate: false, formatter: function (value, row, index) {
                                return '<span class="sid" data-id="'+row.source_id+'" data-url="'+row.controller+'">'+value+'</span>';
                            }},
                        {field: 'title', title: __('商品标题')},
                        {field: 'number', title: __('number')},
                        {field: 'type', title: __('单据类型')},
                        {field: 'operator', title: __('operator')},
                        {field: 'company', title: __('company'),  operate: false},
                        {field: 'goods_num', title: __('销售数量')},
                        {field: 'sale_price', title: __('销售单价')},
                        {field: 'ht_price', title: __('含税单价')},
                        {field: 'sfhj', title: __('税费合计')},
                        {field: 'nickname', title: __('所属客户')},
                        {field: 'createtime', title: __('Create time'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        // {field: 'explain', title: __('explain')},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            buttons: [

                            ],
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });
            // $('table').on('dblclick','td',function(){
            //     var source_id = $(this).parents('tr').find('.sid').data('id')
            //     var turl = $(this).parents('tr').find('.sid').data('url')
            //     var url = Config.this_url+'/'+turl+'?ids='+source_id
            //     layer.open({
            //         type: 2, // 1-在content中写html代码， 2-在content中写src路径
            //         area: ['80%', '90%'],
            //         title: "销售单详情",
            //         content: url,
            //         // content: Config.this_url+'/purchase/requisition',
            //     })
            // });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});